import { INIT_ISAUTHENTICATED, SET_ISAUTHENTICATED } from "../constant";

export interface AppState {
  isAuthenticated: boolean;
}

export interface AppGetter {
  isAuthenticated: boolean;
}

const app = {
  namespace: true,
  state() {
    return {
      isAuthenticated: !!sessionStorage.getItem("my-app-token"),
    };
  },
  getters: {
    title(state: AppState, getters: AppGetter) {
      return state.isAuthenticated;
    },
  },
  mutations: {
    // 同步方法
    [SET_ISAUTHENTICATED](state: AppState, token: string) {
      state.isAuthenticated = !!token;
      sessionStorage.setItem("my-app-token", token);
    },
    [INIT_ISAUTHENTICATED](state: AppState) {
      state.isAuthenticated = false;
      sessionStorage.setItem("my-app-token", "");
    },
  },
};

export default app;
